package edu.cueb.wmis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.cueb.wmis.mapper.GoodMapper;
import edu.cueb.wmis.mapper.OrderbillMapper;
import edu.cueb.wmis.mapper.PandiandetailMapper;
import edu.cueb.wmis.pojo.Incomebill;
import edu.cueb.wmis.pojo.Orderbill;
import edu.cueb.wmis.pojo.Orderbillitem;
import edu.cueb.wmis.mapper.OrderbillitemMapper;
import edu.cueb.wmis.pojo.Pandiandetail;
import edu.cueb.wmis.service.IOrderbillitemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@Service
public class OrderbillitemServiceImpl extends ServiceImpl<OrderbillitemMapper, Orderbillitem> implements IOrderbillitemService {

    @Autowired
    private OrderbillitemMapper orderbillitemMapper;
    @Autowired
    private OrderbillMapper orderbillMapper;
    @Autowired
    private GoodMapper goodMapper;

    @Override
    public boolean addOrderBillItem(Orderbillitem orderbillitem) {
        List<Map<String, Object>> list = orderbillMapper.selectMaps(new QueryWrapper<Orderbill>().select("max(id)"));
        Integer billId = Integer.parseInt(list.get(0).get("max(id)").toString());  //找到最新的概单id
        orderbillitem.setBillId(billId);
        Integer number = orderbillitem.getNumber();  //number用户手输
        Integer goodId = orderbillitem.getGoodId();  //goodId是通过下拉框选择的
        orderbillitem.setGood(goodMapper.selectById(goodId));  //把good对象赋过，便于前端乱七八糟的操作。
        BigDecimal price = orderbillitem.getGood().getPrice();
        orderbillitem.setAmount(price.multiply(new BigDecimal(number.toString())));
        System.out.println(orderbillitem);
        if(orderbillitemMapper.insert(orderbillitem)==1){
            return true;
        }else{
            return false;
        }
    }


}
